package lib.pacificapp.widget;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.internal.MyScrollView;
 
import android.content.Context; 
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class DataPanel extends  LinearLayout {
    public interface OnDataListener{
    	   void onRefresh();
    	   void onLoadmore(); 
    }
    
    public void completed(){
    	 if(pulltorefresh!=null)
    		 pulltorefresh.onRefreshComplete();
    }
    public void init(){
    	  initView();
    }
    private OnDataListener mOnDataListener;
    public void setOnDataListener(OnDataListener listener){
    	mOnDataListener=listener;
    }
    public DataPanel(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		 
	}

	public DataPanel(Context context, AttributeSet attrs) {
		super(context, attrs);
//		try{
//			initView();
//			}finally{}
		// TODO Auto-generated constructor stub
	}
   
	/***刷新控件*/
	private PullToRefreshScrollView pulltorefresh;
	public PullToRefreshScrollView getTarget(){
	  	return pulltorefresh;
	}
	private View tmpView;
	private void initView(){
        tmpView=getChildAt(0);
        this.removeAllViews();
		pulltorefresh=new PullToRefreshScrollView(getContext());
		LinearLayout.LayoutParams ps=new LinearLayout.LayoutParams(-1, -1);
		this.addView(pulltorefresh,ps);
		pulltorefresh.getLoadingLayoutProxy().setLastUpdatedLabel("最后更新在1小时前");   
		pulltorefresh.getLoadingLayoutProxy().setPullLabel("下拉刷新");  
		pulltorefresh.getLoadingLayoutProxy().setRefreshingLabel("正在刷新");  
		pulltorefresh.getLoadingLayoutProxy().setReleaseLabel("松开刷新");  
         //上拉、下拉设定  
		pulltorefresh.setMode(Mode.BOTH);  
		 
		 
		pulltorefresh.setOnRefreshListener(new OnRefreshListener<MyScrollView>(){
			@Override  
			public void onRefresh(PullToRefreshBase<MyScrollView> arg0) {
				// TODO Auto-generated method stub
				if(pulltorefresh.isHeaderShown()){
					if(mOnDataListener!=null)
						mOnDataListener.onRefresh();
					
				}else if(pulltorefresh.isFooterShown()){
					if(mOnDataListener!=null)
						mOnDataListener.onLoadmore();
				}
				
			    //mFocusPanel.onRefreshComplete(); 
			    
			}});
          
  
        //获取ScrollView布局，此文中用不到  
		ScrollView mScrollView = pulltorefresh.getRefreshableView(); 
		if(mScrollView!=null){
		mScrollView.addView(tmpView);
		}else{
			 Log.d("debug","null");
			
		}
	}
	

}
